package com.unidt.www.module.expert.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.unidt.www.framework.mybatis.core.mapper.BaseMapperX;
import com.unidt.www.module.expert.domain.ExpertRuleUserDo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface ExpertRuleUserMapper extends BaseMapperX<ExpertRuleUserDo> {

    default List<ExpertRuleUserDo> getByRuleIdAndMessageStatus(Long ruleId) {
        LambdaQueryWrapper<ExpertRuleUserDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getRuleId, ruleId);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getSendStatus,1);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getDeleted,false);
        lambdaQueryWrapper.orderByDesc(ExpertRuleUserDo::getUpdateTime);
        return this.selectList(lambdaQueryWrapper);
    }

    default Long countByRuleId(Long ruleId) {

        LambdaQueryWrapper<ExpertRuleUserDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getRuleId, ruleId);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getDeleted, false);
        return this.selectCount(lambdaQueryWrapper);
    }

    default Long countByInvitationIdAndStatus(Long invitationId,Integer status) {

        LambdaQueryWrapper<ExpertRuleUserDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getInvitationId, invitationId);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getDeleted, false);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getStatus,status);
        return this.selectCount(lambdaQueryWrapper);
    }

    default List<ExpertRuleUserDo> getByRuleIdAndStatus(Long ruleId, Integer status) {
        LambdaQueryWrapper<ExpertRuleUserDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getRuleId, ruleId);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getSendStatus,1);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getDeleted,false);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getStatus, status);
        lambdaQueryWrapper.orderByDesc(ExpertRuleUserDo::getUpdateTime);
        return this.selectList(lambdaQueryWrapper);
    }

    Integer countEffective(@Param(value = "ruleId") Long ruleId);

    default  List<ExpertRuleUserDo> getSpareList(Long ruleId){
        LambdaQueryWrapper<ExpertRuleUserDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getRuleId, ruleId);
        lambdaQueryWrapper.isNull(ExpertRuleUserDo::getSendTime);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getDeleted,false);
        /**
         * 确保被 替换候选首先发送
         */
        lambdaQueryWrapper.orderByDesc(ExpertRuleUserDo::getIsReplace);
        return this.selectList(lambdaQueryWrapper);
    }

   default ExpertRuleUserDo getByCode(String code){
       LambdaQueryWrapper<ExpertRuleUserDo> lambdaQueryWrapper =
               new LambdaQueryWrapper<>();
       lambdaQueryWrapper.eq(ExpertRuleUserDo::getInvitationCode, code);
       lambdaQueryWrapper.eq(ExpertRuleUserDo::getDeleted,false);
       return this.selectOne(lambdaQueryWrapper);
   }

    default ExpertRuleUserDo getByRuleIdAndExpertId(Long ruleId,Long expertId){
        LambdaQueryWrapper<ExpertRuleUserDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getRuleId, ruleId);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getExpertId,expertId);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getDeleted,false);
        return this.selectOne(lambdaQueryWrapper);
    }

    default List<ExpertRuleUserDo> getLeftList(Long ruleId){
        LambdaQueryWrapper<ExpertRuleUserDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getRuleId, ruleId);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getSendStatus,0);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getIsReplace,false);
        lambdaQueryWrapper.eq(ExpertRuleUserDo::getDeleted,false);
        return this.selectList(lambdaQueryWrapper);
    }
}